#!/bin/bash
#
# atool-extract
# Tuesday, 04/21/2020
# by Paul Sherman for Absolute Linux, 2018-20 - GPL3
#
if [ -z "$1" ] || [ ! -f "$1" ] 
  then
    echo "No file name supplied"
	exit 0
fi

DIR=$(dirname "${1}")
x=`echo "$1" | sed 's/\.gz//g' | sed 's/\.xz//g' | sed 's/\.bz2//g'`
foldname="${x%.*}"
#echo $foldname
cd "$DIR"
if [ -d "$foldname" ]; then
	zenity  --width=250 --question --text="$foldname already exists,\ndo you wish to overwrite that folder?"
	if [ $? != 0 ]; then
		exit 0
	else
		rm -rf "$foldname"
	fi
fi



# tar vs compressed tar snafu avoidance...
# ends with .tar.gz
x=`echo "$1" | grep '.tar.gz$'`
if [ ! -z "$x" ];then
	# ends with .tar.gx...
	# but is it really compressed?
	y=`file "$1" | grep 'POSIX tar archive$'`
	if [ ! -z "$y" ];then
		#s this is a .tar.gz, but is NOT compressed....
		echo "This is a tar.gz, but is not compressed"
		tar -xvf "$1"
		exit 0
	fi
fi



aunpack "$1" | zenity --width=400 --progress --pulsate --window-icon="/usr/share/pixmaps/extract.svg" --auto-close --text="Unpacking..."  2> /dev/null
exit 0
